package org.kl.bf.service.basic;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.kl.bf.entity.basic.BasicFile;
import org.kl.bf.repository.basic.BasicFileDao;
import org.kl.bf.utils.CRUDUtils;
import org.kl.bf.utils.KLDynamicSpecifications;
import org.kl.bf.utils.KLSearchFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author hqm
 * @CreateTime 2014-3-13 下午9:11:30
 */
// Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional
public class BasicFileService {

	private BasicFileDao basicFileDao;

	public BasicFile getBasicFile(String id) {
		return basicFileDao.findOne(id);
	}

	public void saveBasicFile(BasicFile entity) {
		basicFileDao.save(entity);
	}

	public void deleteBasicFile(String id) {
		basicFileDao.delete(id);
	}

	public void delJoinFileId(String id) {
		BasicFile basicFile = basicFileDao.findOne(id);
		basicFile.setJoinFileId("");
		basicFileDao.save(basicFile);

	}

	public List<BasicFile> getBasicFileByJoinFileId(String joinFileId) {
		return basicFileDao.findByJoinFileId(joinFileId);
	}

	/**
	 * 删除多条记录
	 * 
	 * @param ids
	 */
	public String multiDelete(String ids) {
		String[] idArray = StringUtils.split(ids, ",");
		int i = 0, j = 0;
		for (String id : idArray) {
			if (StringUtils.isNotBlank(id)) {
				BasicFile b = getBasicFile(id);
				if (DeleteFolder(b.getFilePath())) {
					basicFileDao.delete(id);
					i++;
				} else {
					j++;
				}
			}
		}
		return "删除" + i + "条记录，" + j + "条文件的记录删除失败";
	}

	/**
	 * 根据路径删除指定的目录或文件，无论存在与否
	 * 
	 * @param sPath
	 *            要删除的目录或文件
	 * @return 删除成功返回 true，否则返回 false。
	 */
	public boolean DeleteFolder(String sPath) {
		Boolean flag = false;
		File file = new File(sPath);
		// 判断目录或文件是否存在
		if (!file.exists()) { // 不存在返回 false
			return flag;
		} else {
			// 判断是否为文件
			if (file.isFile()) { // 为文件时调用删除文件方法
				return deleteFile(sPath);
			} else {
				return flag;
			}
		}
	}

	/**
	 * 删除单个文件
	 * 
	 * @param sPath
	 *            被删除文件的文件名
	 * @return 单个文件删除成功返回true，否则返回false
	 */
	public boolean deleteFile(String sPath) {
		Boolean flag = false;
		File file = new File(sPath);
		// 路径为文件且不为空则进行删除
		if (file.isFile() && file.exists()) {
			file.delete();
			flag = true;
		}
		return flag;
	}

	public List<BasicFile> getAllBasicFile() {
		return basicFileDao.findAll();
	}

	public Page<BasicFile> getUserBasicFile(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType,
			String orderType) {
		PageRequest pageRequest = CRUDUtils.buildPageRequest(pageNumber, pageSize, sortType, orderType);
		Specification<BasicFile> spec = buildSpecification(searchParams);

		return basicFileDao.findAll(spec, pageRequest);
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<BasicFile> buildSpecification(Map<String, Object> searchParams) {
		Map<String, KLSearchFilter> filters = KLSearchFilter.parse(searchParams);
		Specification<BasicFile> spec = KLDynamicSpecifications.byKLSearchFilter(filters.values(), BasicFile.class);
		return spec;
	}

	@Autowired
	public void setBasicFileDao(BasicFileDao basicFileDao) {
		this.basicFileDao = basicFileDao;
	}

}
